#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>
#include "tree.h"

using namespace std;

void inOrderTraverse(Tree *root, vector<int> &inOrder)
{
    if (!root) return;
    
    inOrder.push_back(root->data);
    inOrderTraverse(root->left, inOrder);
    inOrderTraverse(root->right, inOrder);
}

Tree *reconstruct(const vector<int> &inOrder, int &index, int lo, int hi)
{
    if (index == inOrder.size()) return NULL;
    if (inOrder[index] <  lo || inOrder[index] > hi) return NULL;
    Tree *parent = new Tree(inOrder[index++]); 
    parent->left = reconstruct(inOrder, index, lo, parent->data);
    parent->right = reconstruct(inOrder, index, parent->data, hi);
    return parent;
}


int main(int argc, char **argv)
{
    int input[] = {21,7,5,6,3,2,1,4,6,11,35,56};
    int input2[] = {6,23,43,54,12,54,8,10,11,35,56};
    vector<int> inOrder;
    //int input[] = {7};
    Tree *root = NULL, *root2 = NULL;
    int result;

    for (int i = 0; i < sizeof(input)/sizeof(int); i++) {
        TreeInsert(root, input[i]);
    }

    print_ascii_tree(root);
    inOrderTraverse(root, inOrder);
    for (int i = 0; i < inOrder.size(); i++)
        cout << inOrder[i] << " ";
    int index = 0;
    cout << endl;
    Tree *t = reconstruct(inOrder, index, -1000000, 100000000);
    print_ascii_tree(t);
}
